package com.qianfeng.alibaba.consumer.controller;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * Created by jackiechan on 20-8-5 上午11:15 没有文学素养长得丑并且耳聋的张俊波
 *
 * @Author jackiechan
 */
@RestController
@RefreshScope
public class ConsumerController {


  private   Random random=  new Random();
    @Autowired
    private RestTemplate restTemplate;

    @Value("${nacos.name}")
    private String name;

    @RequestMapping("/baoxian")
    @SentinelResource(value = "abc",blockHandler ="yinggaicaidaole",fallback ="niyeyinggaicaidaole" )
    public String test1() {
       return restTemplate.getForObject("http://01provider/test1", String.class);
    }

    @RequestMapping("/getname")
    @SentinelResource(value = "abc",blockHandler ="yinggaicaidaole",fallback ="niyeyinggaicaidaole" )
    public String getName() {
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return name;
    }

    @RequestMapping("/testa")
    public String testA() {
        return "testA";
    }



    @RequestMapping("/testb")
    public String testB() {
        int nextInt = random.nextInt(4);
        if (nextInt == 0) {
            int i = 1 / 0;
        }
        return "testbbbbb";
    }

    @SentinelResource(value = "testc", blockHandler = "yinggaicaidaole", fallback = "niyeyinggaicaidaole")
    @RequestMapping("/testc")
    public String testc(String name, Integer age) {

        return "testcccccccc" + name + age;
    }


    public String yinggaicaidaole( BlockException exception) {
        System.err.println(exception);
        return "zhe shi liu de tong de fan kun de zi shi ";
    }

    public String niyeyinggaicaidaole() {
        return "niyeyinggaicaidaole";
    }
}
